/**
 * Created by cuppi on 2017/7/18.
 */

let host = '139.196.138.67';
let user = 'root';
let password = '7l_X_0qYhP';
let database = 'test';
const TABLE_NAME = 'authentication_user';
class UserFaceDAO{

    static getDBConnection() {
        var mysql = require('mysql');
        var connection = mysql.createConnection({
            host: host,
            user: user,
            password: password,
            database: database
        });
        connection.connect();
        return connection;
    }

    static queryPromise(connection, query) {
        return new Promise((resolve, reject) => {
            connection.query(query, function (error, results, fields) {
                if (error) {
                    reject(error);
                    return;
                }
                resolve(results);
            });
        });
    }

    static isHasUserFace(userId){
        let connection = this.getDBConnection();
        return new Promise((resolve, reject) => {
            this.queryPromise(connection, {
                sql: 'SELECT COUNT(*) AS count from ?? WHERE user_id = ?',
                values: [TABLE_NAME, userId]
            }).then(results => {
                connection.end();
                resolve(results[0].count > 0);
            }, error => {
                connection.end();
                reject(error);
            });
        });
    }

    static getUserFaceList() {
        let connection = this.getDBConnection();
        return new Promise((resolve, reject) => {
            this.queryPromise(connection, {
                sql: 'SELECT * FROM ??',
                values: [TABLE_NAME]
            }).then(list => {
                connection.end();
                let smartList = list.map(userFace => {
                    let userId = userFace.user_id;
                    let userGroup = userFace.user_group;
                    let userFaceUrl = userFace.user_face_url;
                    let isUseabel = userFace.is_useable;
                    let other = userFace.other;
                    let createTime = userFace.create_time;
                    return {userId, userGroup, userFaceUrl, isUseabel, other, createTime};
                });
                resolve(smartList);
            }, error => {
                connection.end();
                console.log(error);
            });
        });
    }

    static addUserFace(userFace){
        if (!userFace){
            return this.failedPromise(new Error('错误的用户人脸'));
        }
        let userId = userFace.userId;
        let userGroup = userFace.userGroup;
        let userFaceUrl = userFace.userFaceUrl;
        if (!userId || !userGroup || !userFaceUrl){
            return this.failedPromise(new Error('错误的用户人脸'));
        }

        return new Promise((resolve, reject) => {
            this.isHasUserFace(userId).then(isHas => {
                if (!isHas){
                    let connection = this.getDBConnection();
                    this.queryPromise(connection, {
                        sql: 'INSERT INTO ?? (user_id, user_group, user_face_url) VALUES (?, ?, ?)',
                        values: [TABLE_NAME, userId, userGroup, userFaceUrl]
                    }).then(result => {
                        connection.end();
                        resolve(result);
                    }, error => {
                        connection.end();
                        reject(error);
                    });
                } else {
                    reject(new Error('用户已经存在'));
                }
            },  error => {
                reject(error);
            })
        })
    }

    static failedPromise(error){
        return new Promise((resolve, reject) => {
            reject(error);
        });
    }
}

module.exports = UserFaceDAO;